using System;
using Moq;
using NUnit.Framework;
using Tycho.Parser.Peg;
using Tycho.Parser.Syntax;

namespace Tycho.Parser.Tests.Syntax {
    [TestFixture]
    public class OptionalSyntaxSpecification {
        [Test]
        public void AccumulateCapturesShouldDelegateToSubSyntax () {
            const string captureName = "name";

            var accumulator = new CaptureAccumulator ();

            OptionalSyntax syntax = new OptionalSyntax (new CaptureSyntax (captureName, null));

            syntax.AccumulateCaptures (accumulator);

            Assert.AreEqual (false, accumulator.Captures[captureName]);
        }

        [Test]
        public void CompileShouldResetIsFirstBackToInitialValueTrue () {
            var optional = new OptionalSyntax (new FakeSetIsFirstSyntax (false));

            var productionLookup = new ProductionLookup (null, null, null);
            Assert.IsTrue (productionLookup.IsFirst);
            optional.Compile (productionLookup);
            Assert.IsTrue (productionLookup.IsFirst);
        }

        [Test]
        public void CompileShouldResetIsFirstBackToInitialValueFalse () {
            var optional = new OptionalSyntax (new FakeSetIsFirstSyntax (false));

            var productionLookup = new ProductionLookup (null, null, null);
            productionLookup.IsFirst = false;
            optional.Compile (productionLookup);
            Assert.IsFalse (productionLookup.IsFirst);
        }
    }

    internal class FakeSetIsFirstSyntax : Parser.Syntax.Syntax {
        public bool IsFirstToSet;

        public FakeSetIsFirstSyntax (bool isFirstToSet) {
            IsFirstToSet = isFirstToSet;
        }

        public override Production Compile (ProductionLookup productionLookup) {
            productionLookup.IsFirst = IsFirstToSet;
            return null;
        }

        public override void AccumulateCaptures (ICaptureAccumulator accumulator) {
            throw new System.NotImplementedException ();
        }
    }
}